package com.learning.security.dal.mysql;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.learning.security.dal.dataobject.AdminUserDO;
import com.learning.security.dal.dataobject.AdminUserRoleDO;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AdminUserMapper extends BaseMapper<AdminUserDO> {

    default AdminUserDO getUserByUsername(String username) {
        LambdaQueryWrapper<AdminUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminUserDO::getUsername, username);
        return selectOne(queryWrapper);
    }

    default boolean assignRole(Long userId, Long roleId) {
        return Db.save(new AdminUserRoleDO().setUserId(userId).setRoleId(roleId));
    }
}
